package com.hspedu.exit_;

/**
 * @author DL5O
 * @version 1.0
 */
public class ThreadExit_ {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();

        t.start();
        //如果希望main线程去控制t1线程的中止，可以修改loop
        //让t1 退出run方法，从而终止 t1线程 -> 通知方式
        System.out.println("主线程休眠10秒");
        Thread.sleep(10 * 1000);
        t.setLoop(false);

    }
}

class T extends Thread{
    private int count = 0;
    private boolean loop = true;
    @Override
    public void run() {
        while(loop){
            try {
                Thread.sleep(50);//休眠50毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T 运行中" + (++count));
        }
    }

    public void setLoop(boolean loop){
        this.loop = loop;
    }
}